package com.order.service.impl;

import com.order.constant.MessageConstant;
import com.order.dto.AdminLoginDTO;
import com.order.dto.UserLoginDTO;
import com.order.entity.Admin;
import com.order.entity.User;
import com.order.exception.AccountNotFoundException;
import com.order.exception.PasswordErrorException;
import com.order.mapper.AdminMapper;
import com.order.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Admin login(AdminLoginDTO adminLoginDTO) {
        String username = adminLoginDTO.getUsername();
        String password = adminLoginDTO.getPassword();


        Admin admin = adminMapper.getByUsername(username);

        if (admin == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //password = DigestUtils.md5DigestAsHex(password.getBytes());
        if(!admin.getPassword().equals(password)){
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        return admin;
    }
}
